package cn.hnu.qq.client.controller;

import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import cn.hnu.qq.entity.Crowd;
import cn.hnu.qq.entity.TaskEvent;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.AddFriendMessage;
import cn.hnu.qq.entity.message.DeleteFriendMessage;
import cn.hnu.qq.entity.message.DownLoadFileMessage;
import cn.hnu.qq.entity.message.ExitCrowdMessage;
import cn.hnu.qq.entity.message.JoinCrowdMessage;
import cn.hnu.qq.entity.message.SendFileMessage;
import cn.hnu.qq.entity.message.UploadFileMessage;
import cn.hnu.qq.global.FileReceiver;
import cn.hnu.qq.global.FileSender;
import cn.hnu.qq.global.MessageType;

public class FriendsListController extends CenterController{
	
	private static FriendsListController instance = null;
	
	private ServerSocket ss ; //文件发送端
	
	private Socket s;

	private String selectedFile; //文件接收端
	
	private FriendsListController(){};
	
	public static synchronized FriendsListController getInstance(){
		if(instance == null){
			instance = new FriendsListController();
		}
		return instance;
	}

	public void confirmAddFriend(TaskEvent task) {
		AddFriendMessage afm = new AddFriendMessage(MessageType.CONFIRM_ADD_FRIEND);
		afm.setFriend(task.getDealer().getAcount());
		afm.setUser(task.getMaker().getAcount());
		try {
			getOutput().writeObject(afm);
			getOutput().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void confirmJoinCrowd(TaskEvent task) {
		JoinCrowdMessage jcm = new JoinCrowdMessage(MessageType.CONFIRM_JOIN_CROWD);
		jcm.setCrowd(task.getSource());
		jcm.setUser(task.getMaker().getAcount());
		try {
			getOutput().writeObject(jcm);
			getOutput().flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public void deleteFriend(User user) {
		DeleteFriendMessage dfm = new DeleteFriendMessage();
		dfm.setUser(getCurUser().getAcount());
		dfm.setFriend(user.getAcount());
		try {
			getOutput().writeObject(dfm);
			getOutput().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void exitCrowd(Crowd crowd) {
		ExitCrowdMessage ecm = new ExitCrowdMessage();
		ecm.setCrowd(crowd.getCrowdName());
		ecm.setUser(getCurUser().getAcount());
		try {
			getOutput().writeObject(ecm);
			getOutput().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sendFileMessage(User user, File file) {
		SendFileMessage sfm = new SendFileMessage();
		sfm.setChoose(SendFileMessage.NOT_CHOOSE);
		sfm.setFileName(file.getName());
		sfm.setFileSize(file.length());
		sfm.setUser(getCurUser().getAcount());
		sfm.setAbsPath(file.getAbsolutePath());
		byte[] data = ObjectToArray(sfm);
		try {
			DatagramPacket dp = new DatagramPacket(data, data.length, InetAddress.getByName(user.getIp()), user.getUserClientPort());
			getdSocket().send(dp);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

	public FileReceiver setupSendFile(User user, SendFileMessage sfm2) {
		SendFileMessage sfm = new SendFileMessage();
		sfm.setChoose(SendFileMessage.OK);
		FileReceiver fr = null;
		try {
			sfm.setIp(InetAddress.getLocalHost().getHostAddress());
			fr = new FileReceiver();
			int port = 20000;
			while(!fr.bind(port)){
				port++;
			}
			sfm.setPort(port);
			sfm.setAbsPath(sfm2.getAbsPath());
			byte[] data = ObjectToArray(sfm);
			System.err.println("seuup use is:"+user.getAcount()+user.getIp()+":"+user.getUserClientPort());
			DatagramPacket dp = new DatagramPacket(data, data.length,InetAddress.getByName(user.getIp()),user.getUserClientPort());
			getdSocket().send(dp);
		} catch (Exception e){
			e.printStackTrace();
		}
		return fr;
	}

	public FileSender sendFile(SendFileMessage sfm) {
		File f = new File(sfm.getAbsPath());
		FileSender fs = new FileSender(f,sfm.getIp(), sfm.getPort());
		fs.start();
		return fs;
	}
	
	public void sendUploadFileMessage(File f){
		UploadFileMessage ufm = new UploadFileMessage();
		ufm.setUploader(getCurUser().getAcount());
		ufm.setFilePath(f.getAbsolutePath());
		try {
			getOutput().writeObject(ufm);
			getOutput().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public FileSender sendFile(UploadFileMessage ufm){
		File f = new File(ufm.getAbsolutePath());
		FileSender fs = new FileSender(f,clientProperties.getProperty("server.ip"), ufm.getPort());
		fs.start();
		return fs;
	}

	public String getServerIp() {
		return clientProperties.getProperty("server.ip");
		
	}
	
	public void sendDownLoadMessage(){
		DownLoadFileMessage dlfm = new DownLoadFileMessage();
		dlfm.setAcount(getCurUser().getAcount());
		
		try {
			getOutput().writeObject(dlfm);
			getOutput().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setSelectedFile(String selectedValue) {
		this.selectedFile = selectedValue;
	}

	public String getSelectedFile() {
		return selectedFile;
	}
	
}
